package com.sqy.read;

import com.sqy.common.Log;
import com.sqy.common.Num;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadTxt {

    //private static String encoding = "UTF-8";


    public static void read(String path, String encoding) throws IOException {
        File file = new File(path);

        InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
        BufferedReader bufferedReader = new BufferedReader(read);

        //上一行，防止目的字串被分行
        String preLine = null;
        //当前行
        String lineText = null;

        //所在行
        int line = 0;
        //该文件多少次
        int count = 0;

        while ((lineText = bufferedReader.readLine()) != null) {
            //System.out.println(lineText);

            line++;

            //1.创建匹配模式
            //涉密、秘密、机密、绝密、保密、泄密
            String pattern = Num.TARGET_STR;
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(preLine + lineText);

            //boolean isMatch=m.find();
            while (m.find()) {
                count++;
                //System.out.println("第" + (++count) + "次找到");
                //System.out.println("在第"+line+"行");
            }
            if (lineText.length() > 0) {
                preLine = lineText.substring(lineText.length() - 1);
            } else {
                preLine = "";
            }


        }
        read.close();

        if (count > 0) {
            Num.Targetfile++;
        }
        System.out.println("该文件共" + count + "处疑似泄密");

        //写入日志文件
        String content = "该文件共" + count + "处疑似泄密";
        Log.log(content);

    }
}
